package cz.tomasdvorak.cars.pages.expenses;

import cz.tomasdvorak.cars.auth.Auth;
import cz.tomasdvorak.cars.auth.MyAuthenticatedWebSession;
import cz.tomasdvorak.cars.dto.Expense;
import cz.tomasdvorak.cars.dto.ExpenseCriteria;
import cz.tomasdvorak.cars.dto.User;
import cz.tomasdvorak.cars.pages.BasePage;
import cz.tomasdvorak.cars.utils.Lists;
import cz.tomasdvorak.cars.wicket.components.table.AbstractColumnCss;
import cz.tomasdvorak.cars.wicket.components.table.AbstractListDataProvider;
import cz.tomasdvorak.cars.wicket.components.table.EnhancedDataTable;
import cz.tomasdvorak.cars.wicket.components.table.PropertyColumnCss;
import cz.tomasdvorak.cars.wicket.components.breadcrumbs.BreadcrumbItem;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import java.util.List;

@AuthorizeInstantiation(Auth.USER)
public class AllExpensesPage extends BasePage {
    private static final int ITEMS_PER_PAGE = 20;

    private class ExpensesProvider extends AbstractListDataProvider<Expense> {

        protected List<Expense> loadData() throws Exception {
            return AllExpensesPage.this.getExpensesDao().getAll(new ExpenseCriteria());
        }
    }

private class ActionsFragment extends Fragment {

        public ActionsFragment(String id, String markupId, MarkupContainer markupProvider, final IModel<Expense> model) {
            super(id, markupId, markupProvider, model);
            final User user = ((MyAuthenticatedWebSession) getSession()).getUser();
            Link editLink = new Link("editLink") {
                public void onClick() {
                    setResponsePage(new ExpenseEditingPage(model));
                }
            };
            editLink.setVisible(user.isAdmin() || user.getId() == model.getObject().getPersonId());
            add(editLink);
        }
    }


    public AllExpensesPage() {
         IColumn<Expense>[] columns = Lists.array(
                 new PropertyColumnCss<Expense>("col1", new ResourceModel("type"),"type"),
                 new PropertyColumnCss<Expense>("col2", new ResourceModel("date"),"date"),
                 new PropertyColumnCss<Expense>("col3", new ResourceModel("mileage"),"mileage"),
                 new PropertyColumnCss<Expense>("col5", new ResourceModel("price"),"price"),
                 new AbstractColumnCss<Expense>("col6", new ResourceModel("personId"),"personId"){
                     public void populateItem(Item<ICellPopulator<Expense>> cellItem,
                                              String componentId, IModel<Expense> rowModel) {
                         String user = "";
                         int person = rowModel.getObject().getPersonId();
                         if (person > -1) {
                             User userObj = AllExpensesPage.this.getUserDao().getUserById(person);
                             if (userObj != null) {
                                 user = userObj.getName();
                             }
                         }
                         cellItem.add(new Label(componentId, user));
                     }
                 },
                 new AbstractColumnCss<Expense>("col7", new ResourceModel("vehicleId"), "vehicleId"){
                    public void populateItem(Item<ICellPopulator<Expense>> cellItem,
                                             String componentId, IModel<Expense> rowModel) {
                        String vehicle = "";
                        int vehicleId = rowModel.getObject().getVehicleId();
                        if(vehicleId > -1) {
                            vehicle = AllExpensesPage.this.getVehiclesDao().getById(vehicleId).toString();
                        }
                        cellItem.add(new Label(componentId, vehicle));
                    }
                },
                 new AbstractColumnCss<Expense>("col8", new ResourceModel("action")){
                    public void populateItem(Item<ICellPopulator<Expense>> cellItem,
                                             String componentId, IModel<Expense> rowModel) {
                        cellItem.add(new ActionsFragment(componentId, "actionsFragment", AllExpensesPage.this, rowModel));
                    }
                }
         );

        EnhancedDataTable<Expense> table = new EnhancedDataTable<Expense>("table", columns,
                new ExpensesProvider(), ITEMS_PER_PAGE);
        add(table);
        add(new Link("addNew"){
            public void onClick() {
                setResponsePage(ExpenseEditingPage.class);
            }
        });
    }

    protected List<BreadcrumbItem> getPageBreadcrumb() throws Exception {
        return Lists.arrayList(new BreadcrumbItem(new ResourceModel("breadcrumb.expenses")));
    }
}
